package algorithm;

/**
 * Description:
 * 给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写
 * @author: chengrongkai
 * Date: 2021/3/11
 */
public class Solution24 {

    public static void main(String[] args) {
        String str = "A man, a plan, a canal: Panama";
        str = "acaaa";
        boolean solution = solution(str);
        System.out.println(solution);
    }


    public static boolean solution(String s){
        char[] chars = s.toCharArray();
        int left = 0;
        int right = chars.length-1;
        boolean result = true;
        while (left < right){
            if (!Character.isDigit(chars[left]) && !Character.isLetter(chars[left])){
                left++;
            }else if (!Character.isDigit(chars[right]) && !Character.isLetter(chars[right])){
                right--;
            }else{
                if (Character.toUpperCase(chars[left]) != Character.toUpperCase(chars[right])){
                    result = false;
                    break;
                }
                left++;
                right--;
            }
        }
        return result;
    }
}
